﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Windows.Forms;

namespace FodeeAdmin.SystemSettings
{
    public class BaseForm:Form
    {
        public virtual bool ValidateForm()
        {
            bool result = true;
            foreach (Control c in this.Controls)
            {
                 if (c is GroupBox)
                {
                    result = ValidateData(c);
                }
            }
            return result;
        }

        private bool ValidateData(Control container)
        {
            int inValidCount = 0;
            foreach (Control c in container.Controls)
            {
                if (c is CustomComponent.RequiredFieldValidator)
                {
                    CustomComponent.RequiredFieldValidator validControl = c as CustomComponent.RequiredFieldValidator;
                    validControl.Validate();
                    if (!validControl.IsValid)
                    {
                        inValidCount++;
                    }
                }
            }

            return inValidCount == 0;
        
        }

        public virtual void ClearForm()
        {
            ClearData(this);
        }

        private void ClearData(Control container)
        {
            foreach (Control c in container.Controls)
            {
                if (c is TextBox)
                {
                    (c as TextBox).Text = string.Empty;
                }
                else if (c is ComboBox)
                {
                    if ((c as ComboBox).Items.Count > 0)
                    {
                        (c as ComboBox).SelectedIndex = 0;
                    }
                }
                else if (c is RichTextBox)
                {
                    (c as RichTextBox).Text = string.Empty;
                }
                else if (c is GroupBox)
                {
                    ClearData(c);
                }
            }
        }

        public enum Operation
        { 
            Add,
            Edit
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // BaseForm
            // 
            this.ClientSize = new System.Drawing.Size(984, 662);
            this.Name = "BaseForm";
            this.ResumeLayout(false);

        }
    }
}
